package lq.common.handler;

import lq.common.entity.StudyResponse;
import lq.common.utils.StudyUtil;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: cloud-study
 * @description: 资源服务401认证失败异常
 * 因为资源服务可能会在多个地方使用,所以在common公共模块中定义
 * 由于common只是普通的maven项目,不是springboot项目
 * 即使使用了@Component注解标注,也不能成功注册到各个微服务ICO容器当中
 * 所以没有使用@Component注解标注
 * 可以使用@Enable模块驱动的方式解决问题
 * @author: LQ
 * @create: 2020-09-29 16:20
 */
public class StudyAuthExceptionEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        StudyResponse response = new StudyResponse();
        StudyUtil.makeResponse(httpServletResponse,MediaType.APPLICATION_JSON_UTF8_VALUE,HttpServletResponse.SC_UNAUTHORIZED,response.message("token无效"));
    }
}
